package com.shp.utils;

import com.shp.pojo.Admin;
import com.shp.pojo.User;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Description:
 *  用户在线统计工具类，依赖于sessionId
 * @author zwl
 * @version 1.0
 * @date 2020/3/18 16:34
 */
public class OnlineUserUtil {

    /**
     * key ： email
     * value : session
     * 储存session   <br/>
     */
    private static HashMap<String,HttpSession> sessionMap = new HashMap<String,HttpSession>();
    /**
     * 储存用户的email
     */
    private static List<String> userEmailList = new LinkedList<String>();


    /**
     * 获取sessionIdList
     * @return List<String>
     */
    public static HashMap<String,HttpSession> getSessionMap() {
        return sessionMap;
    }

    /**
     * 获取userEmailList
     * @return  List<String>
     */
    public static List<String> getUserEmailList() {
        return userEmailList;
    }

    /**
     * 添加用户的email
     * @param email 邮箱
     */
    public static synchronized boolean addUserEmail(String email){
        if(email != null){
            userEmailList.add(email);
            return true;
        }
        return false;
    }

    /**
     * 添加用户的email
     * @param user User
     */
    public static synchronized boolean addUserEmail(User user){
        if(user != null){
            userEmailList.add(user.getEmail());
            return true;
        }
        return false;
    }

    /**
     * 移除指定用户的的email
     * @param email email
     */
    public static synchronized boolean removeUserEmail(String email){
        if(email != null){
            userEmailList.remove(email);
            return true;
        }
        return false;
    }

    /**
     * 移除指定用户的的email
     * @param user User
     */
    public static synchronized boolean removeUserEmail(User user){
        if(user != null && user.getEmail() != null){
            userEmailList.remove(user.getEmail());
            return true;
        }
        return false;
    }

    /**
     * 添加sessionId
     * @param session HttpSession
     */
    public static synchronized boolean add(HttpSession session) {
        if(session != null){
            User user = (User)session.getAttribute("activeUser");
            sessionMap.put(user.getEmail(),session);
            return true;
        }
        return false;
    }

    /**
     * 添加sessionId
     * @param session HttpSession
     */
    public static synchronized boolean addAdmin(HttpSession session) {
        if(session != null){
            Admin admin = (Admin)session.getAttribute("admin");
            sessionMap.put(admin.getUsername(),session);
            return true;
        }
        return false;
    }


    /**
     * 移除session
     * @param session HttpSession
     */
    public static synchronized boolean reduceSession(HttpSession session){
        if(session != null){
            User user = (User)session.getAttribute("activeUser");
            sessionMap.remove(user.getEmail());
            return true;
        }
        return false;
    }

    /**
     * 移除sessionId
     * @param args 用户的唯一标识
     */
    public static synchronized boolean reduceSession(String args){
        if(args != null){
            HttpSession session = sessionMap.get(args);
            sessionMap.remove(args);
            session.invalidate();
            return true;
        }
        return false;
    }

    /**
     * 判断用户是否在线
     * @param args 用户的唯一标识
     * @return true代表在线，否则不在线
     */
    public static boolean isOnline(String args){
        return userEmailList.contains(args);
    }
    /**
     * 获取在在线人数
     * @return 在线人数
     */
    public static Integer getSize(){
        return userEmailList.size();
    }
}
